#include "../views.hpp"


using lms::views::BookView;

BookView::BookView()
{
     option = 0;
     title = "";
     author = "";
     isbn = "";
     editor = "";
     category = "";    
     price = 0.0;
     book = null; 
     UnitOfWork& unitOfWork = UnitOfWork::getInstance();
     bookServices = unitOfWork.GetBookServices();
     
}

Book* BookView::createBook()
{
      Utilities::printText("\n\t\tIntroducing of Books\n");
      title = Utilities::reinterpret_input<string>("\tEnter the Title: "); 
      author = Utilities::reinterpret_input<string>("\tEnter the Author: ");
      isbn = Utilities::reinterpret_input<string>("\tEnter the Isbn: ");
      editor = Utilities::reinterpret_input<string>("\tEnter the Publisher: ");
      category = Utilities::reinterpret_input<string>("\tEnter the Category: ");
      price = Utilities::reinterpret_input<double>("\tEnter the Price: "); 
      
      book = new Book(title,author, isbn, editor, category,price );
      return book;      
}

void BookView::add()
{
    book = createBook(); 
    bookServices->add( book );
}

void BookView::edit()
{
     showBooks();
     option = Utilities::reinterpret_input< int >("\tChoose Which Modify: ");
     option--;
     book = createBook(); 
     bookServices->edit(book,option);
}

void BookView::remove()
{
     showBooks();
     option = Utilities::reinterpret_input< int >("\tChoose Which Delete: ");
     option--;
     bookServices->remove(option);        
}

void BookView::showBooks(){

Node<Book>* i = (bookServices->get())->getFirst();
int x = 1;
        Utilities::printText("\t Lists Of Customers\n");
while(i != NULL){

        cout << "\tBook No." << x++ << "\n";
        cout << "\tTitle: " << i->getData()->getTitle() <<"\n"; 
        cout << "\tAuthor: " << i->getData()->getAuthor() << "\n";
        cout << "\tISBN: " << i->getData()->getIsbn() << "\n";
        cout << "\tPublisher: " << i->getData()->getEditor() << "\n";
        cout << "\tCategory: " << i->getData()->getCategory() << "\n\n";
        cout << "\tPrice: " << i->getData()->getPrice() << "\n\n";
        i = i->getNext();        
  }      
}

void BookView::show(){

    do{
 Utilities::clearScreen(); 
 Utilities::printText("\t Maintenances of Books\n");
 Utilities::printText("\t1. Add : \n");
 Utilities::printText("\t2. List All: \n");
 Utilities::printText("\t3. Modify : \n");
 Utilities::printText("\t4. Delete : \n");
 Utilities::printText("\t5. Back: \n");
 option = Utilities::reinterpret_input<int>("\tChoose The Option: ");
 
 switch(option){
                
 case 1:
  Utilities::clearScreen();   
  add(); 
  show(); 
  break;

 case 2:
  Utilities::clearScreen();
  showBooks();
  Utilities::pause();
  break;
  
 case 3: 
  Utilities::clearScreen(); 
  edit(); 
  show();
  break;
  
 case 4: 
  Utilities::clearScreen();
  remove();
  show(); 
  break;
  
 case 5:
   break;
   
 default:
     Utilities::printText("\tInvalid Option\n");
     Utilities::pause();
     break;  
  
     } 
   }while(option != 5 );
}

